package com.kinon.market.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.market.R;
import com.kinon.market.entity.PandianEntity;
import com.kinon.market.tools.HttpTools;
import com.kinon.market.tools.MyShowDialog;
import com.kinon.market.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author：郑谦
 * @时间：2017-3-28 下午5:38:13
 * @类名：PandianFragment.java
 * @功能: 盘点
 */
public class PandianFragment extends Fragment {

    View jianpanview = null;

    String token;
    //	private TextView tv_amount;
    String str = "";
    String tiaoma = "";
    int amount = 0;
    double total = 0.0;
    String shuliang = "";
    PandianAdapter pandianAdapter;
    HttpTools toolModel = null;
    /**
     * 数字按钮
     */
    private Button btn_9;
    private Button btn_8;
    private Button btn_7;
    private Button btn_6;
    private Button btn_5;
    private Button btn_4;
    private Button btn_3;
    private Button btn_2;
    private Button btn_1;
    private Button btn_0;
    private Button btn_shanchu;
    private Button btn_queren;
    /**
     * 提交
     */
    private Button btn_shoukuan;
    /**
     * 清空
     */
    private Button btn_qingkong;
    /**
     * 编码输入框
     */
    private EditText et_jiesuan;
    /**
     * 扫码输入框
     */
    private EditText et_id;
    /**
     * 数量
     */
    private TextView tv_amount;
    private ListView lv;
    /**
     * 商品信息的集合
     */
    private ArrayList<PandianEntity> pandianlist;
    /**
     * EditText监听
     */
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//	    	Toast.makeText(getActivity(), "2", 1).show();

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
//	    	Toast.makeText(getActivity(), "3", 1).show();

        }

        @Override
        public void afterTextChanged(Editable s) {
//	    	Toast.makeText(getActivity(), "4", 1).show();
            if (et_id.getText().length() == 13) {
//	    		Toast.makeText(getActivity(), "5", 1).show();
//	    		Intent intent = new Intent();
//				intent.putExtra("orderid", et_id.getText().toString());
//				intent.setClass(getActivity(), DingdandetialerweimaActivity.class);
//				startActivity(intent);
                getShangpininfo1(et_id.getText().toString());

//				Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
            }


        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//		window = getActivity().getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
//        window.setAttributes(params);
        return inflater.inflate(R.layout.fragment_pandian, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initRes();
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        et_id.requestFocus();//获取焦点
        et_id.addTextChangedListener(watcher);
    }

    private void initRes() {
        SharedPreferences sp = getActivity().getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        pandianlist = new ArrayList<PandianEntity>();
        lv = (ListView) getView().findViewById(R.id.lv);
        pandianAdapter = new PandianAdapter(getActivity(), pandianlist);
        lv.setAdapter(pandianAdapter);
        et_id = (EditText) getView().findViewById(R.id.et_id);
        btn_9 = (Button) getView().findViewById(R.id.btn_9);
        btn_8 = (Button) getView().findViewById(R.id.btn_8);
        btn_7 = (Button) getView().findViewById(R.id.btn_7);
        btn_6 = (Button) getView().findViewById(R.id.btn_6);
        btn_5 = (Button) getView().findViewById(R.id.btn_5);
        btn_4 = (Button) getView().findViewById(R.id.btn_4);
        btn_3 = (Button) getView().findViewById(R.id.btn_3);
        btn_2 = (Button) getView().findViewById(R.id.btn_2);
        btn_1 = (Button) getView().findViewById(R.id.btn_1);
        btn_0 = (Button) getView().findViewById(R.id.btn_0);
        btn_shanchu = (Button) getView().findViewById(R.id.btn_shanchu);
        btn_queren = (Button) getView().findViewById(R.id.btn_queren);
        btn_shoukuan = (Button) getView().findViewById(R.id.btn_shoukuan);
        btn_qingkong = (Button) getView().findViewById(R.id.btn_qingkong);
        tv_amount = (TextView) getView().findViewById(R.id.tv_amount);
        tv_amount.setText("共计" + amount + "件商品");
//		btn_shoukuan.setText("收款");
        et_jiesuan = (EditText) getView().findViewById(R.id.et_shuru);
        btn_9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "9";
                et_jiesuan.setText(str);
            }
        });
        btn_8.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "8";
                et_jiesuan.setText(str);
            }
        });
        btn_7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "7";
                et_jiesuan.setText(str);
            }
        });
        btn_6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "6";
                et_jiesuan.setText(str);
            }
        });
        btn_5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "5";
                et_jiesuan.setText(str);
            }
        });
        btn_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "4";
                et_jiesuan.setText(str);
            }
        });
        btn_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "3";
                et_jiesuan.setText(str);
            }
        });
        btn_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "2";
                et_jiesuan.setText(str);
            }
        });
        btn_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "1";
                et_jiesuan.setText(str);
            }
        });
        btn_0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "0";
                et_jiesuan.setText(str);
            }
        });
        btn_shanchu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (str.length() == 0) {
                    return;
                } else {
                    str = str.substring(0, str.length() - 1);
                    et_jiesuan.setText(str);
                }
            }
        });
        btn_queren.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                getShangpininfo2();
            }
        });
        btn_shoukuan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                save();
//				getShangpininfo2();
            }
        });
        btn_qingkong.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pandianlist.removeAll(pandianlist);
                pandianAdapter.notifyDataSetChanged();
//				total=0.0;
                amount = 0;
                tv_amount.setText("共计" + amount + "件商品");
//				btn_shoukuan.setText("收款");
            }
        });
    }

    /**
     * 扫码获取商品信息
     */
    private void getShangpininfo1(String tiaoma) {
        if (NetWorkUtil.isNetwork(getActivity()) == false) {
            Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            btn_queren.setEnabled(true);
            return;
        }
        toolModel = new HttpTools(getActivity());
        toolModel.bycode(token, tiaoma,
                new JsonShangpin());
//		Toast.makeText(getActivity(), ""+tiaoma, 1).show();
    }

    /**
     * 输入编码获取商品信息
     */
    private void getShangpininfo2() {
        if (NetWorkUtil.isNetwork(getActivity()) == false) {
            Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            btn_queren.setEnabled(true);
            return;
        }
        btn_queren.setEnabled(false);
        toolModel = new HttpTools(getActivity());
        toolModel.bycode(token, str,
                new JsonShangpin());
//		Log.i("zq", "token--------"+token);
//		Log.i("zq", "str--------"+str);
    }

    /**
     * 提交商品信息
     */
    private void save() {
//		btn_shoukuan.setEnabled(false);
        if (NetWorkUtil.isNetwork(getActivity()) == false) {
            Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
//			btn_queren.setEnabled(true);
            return;
        }
        if (pandianlist.size() == 0) {
            Toast.makeText(getActivity(), "请添加商品！", Toast.LENGTH_SHORT).show();
            return;
        } else {
            toolModel = new HttpTools(getActivity());
            Gson gson = new Gson(); // 实例化Gson对象
            String data = gson.toJson(pandianlist);
            Log.e("zq", "JsonObject-------" + data);
            toolModel.pandiansave(token, data,
                    new JsonSave());
        }

//		Log.i("zq", "token--------"+token);
//		Log.i("zq", "str--------"+str);
    }

    /**
     * 提交入库信息
     */
    private void tijiao(String id) {
        //		btn_shoukuan.setEnabled(false);
        toolModel = new HttpTools(getActivity());
        toolModel.pandianconfirm(token, id, new JsonTijiao());
        //		Log.i("zq", "token--------"+token);
        //		Log.i("zq", "str--------"+str);
    }

    /**
     * 弹出输入键盘
     */
    private void Jianpandialog(final int index, final TextView Text, final TextView textView) {

        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		dialog.setCanceledOnTouchOutside(false);
        jianpanview = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_jianpan, null);
        Button btn_9 = (Button) jianpanview.findViewById(R.id.btn_9);
        Button btn_8 = (Button) jianpanview.findViewById(R.id.btn_8);
        Button btn_7 = (Button) jianpanview.findViewById(R.id.btn_7);
        Button btn_6 = (Button) jianpanview.findViewById(R.id.btn_6);
        Button btn_5 = (Button) jianpanview.findViewById(R.id.btn_5);
        Button btn_4 = (Button) jianpanview.findViewById(R.id.btn_4);
        Button btn_3 = (Button) jianpanview.findViewById(R.id.btn_3);
        Button btn_2 = (Button) jianpanview.findViewById(R.id.btn_2);
        Button btn_1 = (Button) jianpanview.findViewById(R.id.btn_1);
        Button btn_0 = (Button) jianpanview.findViewById(R.id.btn_0);
        Button btn_00 = (Button) jianpanview.findViewById(R.id.btn_00);
        Button btn_dian = (Button) jianpanview.findViewById(R.id.btn_dian);
        Button btn_wancheng = (Button) jianpanview.findViewById(R.id.btn_wancheng);
        Button btn_qingchu = (Button) jianpanview.findViewById(R.id.btn_qingchu);
        final TextView tv_shuliang = (TextView) jianpanview.findViewById(R.id.tv_amount);
//		final String shuliang="";
        dialog.setContentView(jianpanview);
        dialog.show();

        btn_9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "9";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_8.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "8";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "7";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "6";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "5";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "4";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "3";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "2";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "1";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "0";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_00.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = shuliang + "00";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_dian.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (shuliang.length() == 0) {
                    return;
                } else {
                    shuliang = shuliang.substring(0, shuliang.length() - 1);
                    tv_shuliang.setText(shuliang);
                }
            }
        });

        btn_qingchu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                shuliang = "";
                tv_shuliang.setText(shuliang);
            }
        });
        btn_wancheng.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (shuliang.equals("")) {
                    Toast.makeText(getActivity(), "请输入数量!", Toast.LENGTH_SHORT).show();
                    return;
                } else if (Integer.parseInt(shuliang) == 0) {
                    Toast.makeText(getActivity(), "数量必须大于0!", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    amount = amount - Integer.parseInt(Text.getText().toString()) + Integer.parseInt(shuliang);
                    tv_amount.setText("共计" + amount + "件商品");
                    Text.setText(shuliang);
                    pandianlist.get(index).setNew_amount(shuliang);
                    pandianlist.get(index).setOld_amount("" + Integer.parseInt(shuliang) * Double.parseDouble(pandianlist.get(index).getPrice()));
                    textView.setText("￥" + Integer.parseInt(shuliang) * Double.parseDouble(pandianlist.get(index).getPrice()));
                    et_id.requestFocus();
                    shuliang = "";
//					et_id.requestFocus();
                    dialog.dismiss();
                }


            }
        });
    }

    private class JsonShangpin extends JsonHttpResponseHandler {
        boolean flag = false;
        int index;

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            super.onSuccess(statusCode, headers, response);

            try {
                Log.i("info", "商品列表----" + response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject data = response.getJSONObject("data");
                    String id = data.getString("id");
//						String gcategory_id=data.getString("gcategory_id");
                    String code1 = data.getString("code");
                    String name = data.getString("name");
                    String purchaseprice = data.getString("purchaseprice");
//						String refund=data.getString("refund");
//						pandianlist.add(new PandianEntity(id, code1, name, purchaseprice,"1",purchaseprice));
//					Log.e("info","-----3");

                    for (int i = 0; i < pandianlist.size(); i++) {
                        if (pandianlist.get(i).getTiaoma().equals(code1)) {
//							shangpinlist.get(i).setAmount(""+(Integer.parseInt(shangpinlist.get(i).getAmount())+1));
                            index = i;
                            flag = true;
                            continue;
                        }/*else {
                            flag=false;
						}*/
                    }
                    if (flag == true) {
                        pandianlist.get(index).setNew_amount("" + (Integer.parseInt(pandianlist.get(index).getNew_amount()) + 1));
                        pandianlist.get(index).setOld_amount("" + (Double.parseDouble(pandianlist.get(index).getOld_amount()) + Double.parseDouble
                                (pandianlist.get(index).getPrice())));
                    } else {
                        pandianlist.add(0, new PandianEntity(id, code1, name, purchaseprice, "1", purchaseprice));
                    }

                    pandianAdapter = new PandianAdapter(getActivity(), pandianlist);
                    lv.setAdapter(pandianAdapter);
//					pull_dingdan.getRefreshableView().setSelection(offset);
                    lv.setDivider(null);
                    btn_queren.setEnabled(true);
                    str = "";
                    et_jiesuan.setText(str);
                    et_id.setText("");
                    amount = amount + 1;
//					total = total+Double.parseDouble(purchaseprice);
//					btn_shoukuan.setText("收款"+"￥"+total);
                    tv_amount.setText("共计" + amount + "件商品");
//					btn_queren.setEnabled(false);
                    //				Utility.setGridViewHeightBasedOnChildren(pull_twosort);
//					pull_dingdan.onRefreshComplete();
                } else {
                    et_id.setText("");
                    btn_queren.setEnabled(true);
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            Toast.makeText(getActivity(), "网络请求超时", Toast.LENGTH_SHORT).show();
        }
    }

    private class JsonSave extends JsonHttpResponseHandler {
        @Override
        public void onStart() {
            MyShowDialog.showLoadingDialog(getActivity(), "订单生成中....");
            super.onStart();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            MyShowDialog.closeLoadingDialog();
            try {
                Log.i("info", "提交订单----" + response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    btn_shoukuan.setEnabled(true);
                    String data = response.getString("data");
//					Intent intent = new Intent();
//					intent.setClass(getActivity(), JiesuanjineActivity.class);
//					intent.putExtra("total", total);
//					startActivity(intent);
                    tijiao(data);
                    pandianlist.removeAll(pandianlist);
                    pandianAdapter.notifyDataSetChanged();
                    total = 0.0;
                    amount = 0;
                    tv_amount.setText("共计" + amount + "件商品");
//					btn_shoukuan.setText("收款");
//					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                } else {
                    btn_shoukuan.setEnabled(true);
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            MyShowDialog.closeLoadingDialog();
            Toast.makeText(getActivity(), "网络请求超时", Toast.LENGTH_SHORT).show();
        }

    }

    private class JsonTijiao extends JsonHttpResponseHandler {
        //		@Override
//		public void onStart() {
//			MyShowDialog.showLoadingDialog(getActivity(), "入库提交中....");
//			super.onStart();
//		}
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            super.onSuccess(statusCode, headers, response);
//			MyShowDialog.closeLoadingDialog();
            try {
                Log.i("info", "提交订单----" + response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
//					btn_shoukuan.setEnabled(true);
//					String data = response.getString("data");
//					Intent intent = new Intent();
//					intent.setClass(getActivity(), JiesuanjineActivity.class);
//					intent.putExtra("total", total);
//					startActivity(intent);
//					rukulist.removeAll(rukulist);
//					rukuAdapter.notifyDataSetChanged();
//					//					total=0.0;
//					amount=0;
//					tv_amount.setText("共计"+amount+"件商品");
                    //					btn_shoukuan.setText("收款");
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                } else {
//					btn_shoukuan.setEnabled(true);
                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            Toast.makeText(getActivity(), "网络请求超时", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 商品适配器
     */
    public class PandianAdapter extends BaseAdapter {
        Context context;
        ArrayList<PandianEntity> pandianList;
        HttpTools httpTools = null;
        int num;
        int num2;

        public PandianAdapter(Context context, ArrayList<PandianEntity> pandianList) {
            super();
            this.context = context;
            this.pandianList = pandianList;
        }

        public void add(PandianEntity pandianEntity) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return pandianList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return pandianList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHandle handle;
            if (view == null) {
                handle = new ViewHandle();
                view = LayoutInflater.from(context).inflate(R.layout.item_pandian, null);
                handle.tv_tiaoma = (TextView) view.findViewById(R.id.tv_tiaoma);
                handle.tv_name = (TextView) view.findViewById(R.id.tv_name);
                handle.ib_jia = (ImageButton) view.findViewById(R.id.ib_jia);
                handle.ib_jian = (ImageButton) view.findViewById(R.id.ib_jian);
                handle.tv_xiaoji = (TextView) view.findViewById(R.id.tv_yuanyou);
                handle.tv_newamount = (TextView) view.findViewById(R.id.tv_amount);
//				handle.tv_xiaoji=(TextView)view.findViewById(R.id.tv_xiaoji);
//				handle.tv_sname=(TextView)view.findViewById(R.id.tv_sname);
                view.setTag(handle);
            } else {
                handle = (ViewHandle) view.getTag();
            }
            handle.tv_tiaoma.setText(pandianList.get(position).getTiaoma());
            handle.tv_name.setText(pandianList.get(position).getName());
            handle.tv_newamount.setText(pandianList.get(position).getNew_amount());
            handle.tv_xiaoji.setText("￥" + pandianList.get(position).getOld_amount());
//			handle.et_newamount.addTextChangedListener(watcher);
//			handle.et_newamount.addTextChangedListener(new TextWatcher() {
//
//				@Override
//				public void onTextChanged(CharSequence s, int start, int before, int count) {
//					// TODO Auto-generated method stub
////					num=Integer.parseInt(handle.et_newamount.getText().toString());
//
//				}
//
//				@Override
//				public void beforeTextChanged(CharSequence s, int start, int count,
//						int after) {
//					// TODO Auto-generated method stub
//
//				}
//
//				@Override
//				public void afterTextChanged(Editable s) {
//					int num1=Integer.parseInt(pandianList.get(position).getNew_amount());
//					num2=Integer.parseInt((handle.et_newamount.getText().toString()).equals("")?"0":handle.et_newamount.getText().toString());
//					pandianList.get(position).setNew_amount(num2+"");
//					amount=amount-num1+num2;
//					handle.tv_oldamount.setText("￥"+num2*Double.parseDouble(pandianList.get(position).getPrice()));
//					pandianList.get(position).setOld_amount(""+num2*Double.parseDouble(pandianList.get(position).getPrice()));
//					tv_amount.setText("共计"+amount+"件商品");
//					Log.i("zq", "num1---"+num1);
//					Log.i("zq", "num2---"+num2);
//					Log.i("zq", "amount---"+amount);
//				}
//			});
//			TextWatcher watcher = new TextWatcher() {
//
//			    @Override
//			    public void onTextChanged(CharSequence s, int start, int before, int count) {
////			    	Toast.makeText(getActivity(), "2", 1).show();
//
//			    }
//
//			    @Override
//			    public void beforeTextChanged(CharSequence s, int start, int count,
//			            int after) {
//			        // TODO Auto-generated method stub
////			    	Toast.makeText(getActivity(), "3", 1).show();
//
//			    }
//
//			    @Override
//			    public void afterTextChanged(Editable s) {
//			    	handle.et_newamount.getText();
//
//
//			    }
//			};
            handle.tv_newamount.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Jianpandialog(position, handle.tv_newamount, handle.tv_xiaoji);
                }
            });

            handle.ib_jia.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    int goodsNum = Integer.parseInt(handle.tv_newamount.getText().toString());
                    goodsNum++;
                    handle.tv_newamount.setText("" + goodsNum);
                    pandianList.get(position).setNew_amount("" + goodsNum);
                    handle.tv_xiaoji.setText("￥" + Double.parseDouble(pandianList.get(position).getPrice()) * Integer.parseInt(handle.tv_newamount.getText().toString()));
                    amount = amount + 1;
//					total=total+Double.parseDouble(pandianList.get(position).getPrice());
//					btn_shoukuan.setText("收款"+"￥"+total);
                    tv_amount.setText("共计" + amount + "件商品");
                }
            });
            handle.ib_jian.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    int goodsNum = Integer.parseInt(handle.tv_newamount.getText().toString());
                    goodsNum--;
                    if (goodsNum <= 0) {
//						deletegoods(position);
//						goodsNum=0;
//						handle.et_amout.setText("0");
//						amount=amount-1;
//						total=total-Double.parseDouble(pandianList.get(position).getPrice());
//						btn_shoukuan.setText("收款"+"￥"+total);
//						tv_amount.setText("共计"+amount+"件商品");
//						goodsNum--;
//						handle.et_newamount.setText(""+goodsNum);
//						pandianList.get(position).setNew_amount(""+goodsNum);
//						pandianlist.get
                        handle.tv_newamount.setText("" + goodsNum);
                        pandianlist.remove(position);
                        pandianAdapter.notifyDataSetChanged();
                        amount = amount - 1;
                        tv_amount.setText("共计" + amount + "件商品");
                        return;
                    }
                    handle.tv_newamount.setText("" + goodsNum);
                    pandianList.get(position).setNew_amount("" + goodsNum);
                    handle.tv_xiaoji.setText("￥" + Double.parseDouble(pandianList.get(position).getPrice()) * Integer.parseInt(handle.tv_newamount.getText().toString()));
                    pandianList.get(position).setOld_amount("" + Double.parseDouble(pandianList.get(position).getPrice()) * Integer.parseInt(handle.tv_newamount.getText().toString()));
                    amount = amount - 1;
//					total=total-Double.parseDouble(pandianList.get(position).getPrice());
                    tv_amount.setText("共计" + amount + "件商品");

//					amount=amount-1;
//					total=total-Double.parseDouble(pandianList.get(position).getPrice());
//					tv_amount.setText("共计"+amount+"件商品");
                }
            });
//			handle.tv_jine.setText("￥"+Double.parseDouble(caidanList.get(position).getPrice())*Integer.parseInt(caidanList.get(position).getAmount()));
//			if (caidanList.get(position).getSname().equals("")||caidanList.get(position).getSname().equals("null")) {
//				handle.tv_sname.setText("");
//			}else {
//				handle.tv_sname.setText("("+caidanList.get(position).getSname()+")");
//			}

            return view;
        }

        class ViewHandle {
            /**
             * 条码
             */
            TextView tv_tiaoma;
            /**
             * 商品名
             */
            TextView tv_name;
            /**
             * 加
             */
            ImageButton ib_jia;
            /**
             * 现有数量
             */
            TextView tv_newamount;
            /**
             * 减
             */
            ImageButton ib_jian;
            /**
             * 小计
             */
            TextView tv_xiaoji;
        }
    }

}
